import { createRouter, createWebHashHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import sso from "@/config/sso";
import { ElLoading } from "element-plus";
import { clearAllCookies } from "@/utils/auth";
import { useUserStoreWithOut } from "@/store/modules/user";
const userStore = useUserStoreWithOut();

let loadingInstance = null;
const routes = [
  {
    path: "/",
    name: "",
    component: HomeView,
  },
  {
    path: "/service",
    name: "service",
    children: [
      {
        path: "/confirm",
        name: "confirm",
        component: () => import("@/views/service/Confirm.vue"),
      },
      {
        path: "/embedded",
        name: "embedded",
        component: () => import("@/views/service/Embedded.vue"),
      },
      {
        path: "/systemsafet",
        name: "systemsafet",
        component: () => import("@/views/service/Systemsafet.vue"),
      },
      {
        path: "/codesafet",
        name: "codesafet",
        component: () => import("@/views/service/Codesafet.vue"),
      },
      {
        path: "/performance",
        name: "performance",
        component: () => import("@/views/service/Performance.vue"),
      },
      {
        path: "/xingchuang",
        name: "xingchuang",
        component: () => import("@/views/service/Xingchuang.vue"),
      },
      {
        path: "/evaluation",
        name: "evaluation",
        component: () => import("@/views/service/Evaluation.vue"),
      },
      {
        path: "/infosafet",
        name: "infosafet",
        component: () => import("@/views/service/Infosafet.vue"),
      },
      {
        path: "/Wisdom",
        name: "Wisdom",
        component: () => import("@/views/service/Wisdom.vue"),
      },
      {
        path: "/SwiftCode",
        name: "SwiftCode",
        component: () => import("@/views/service/SwiftCode.vue"),
      },
      {
        path: "/SwiftAI",
        name: "SwiftAI",
        component: () => import("@/views/service/SwiftAI.vue"),
      },
      {
        path: "/application",
        name: "application",
        component: () => import("@/views/service/application.vue"),
      },
    ],
  },
  {
    path: "/solution",
    name: "solution",
    children: [
      {
        path: "/universal",
        name: "universal",
        component: () => import("@/views/solution/Universal.vue"),
      },
      {
        path: "/industry",
        name: "industry",
        component: () => import("@/views/solution/Industry.vue"),
      },
    ],
  },
  {
    path: "/about",
    name: "about",
    children: [
      {
        path: "/know",
        name: "know",
        component: () => import("@/views/about/Know.vue"),
      },
      {
        path: "/case",
        name: "case",
        component: () => import("@/views/about/Case.vue"),
      },
    ],
  },
  {
    path: "/privacy",
    name: "",
    component: () => import("@/views/about/Privacy.vue"),
  },
  {
    path: "/orderManage",
    name: "orderManage",
    component: () => import("@/views/order/orderManage.vue"),
    children: [
      {
        path: "order",
        name: "order",
        component: () => import("@/views/order/order.vue"),
      },
      {
        path: "orderDetail",
        name: "orderDetail",
        component: () => import("@/views/order/orderDetail.vue"),
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 如果 `savedPosition` 存在，则表示这是通过浏览器的前进/后退按钮导航，此时返回保存的位置
    if (savedPosition) {
      return savedPosition;
    } else {
      // 否则返回顶部
      return { top: 0 };
    }
  },
});
router.beforeEach(async (to, from, next) => {
  loadingInstance = ElLoading.service({ fullscreen: true });
  if (to.query.quit) {
    // sso的logout不稳定，所以多次清空用户信息
    clearAllCookies();
    userStore.deleteUserCacheAction();
    sso.logout();
  }
  next();
});

router.afterEach(() => {
  // 在路由跳转完成后关闭 loading
  if (loadingInstance) {
    loadingInstance.close();
  }
});
export default router;
